package com.evl.trainSeat.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.evl.common.constant.factory.PageFactory;
import com.evl.common.controller.BaseController;
import com.evl.common.exception.BizExceptionEnum;
import com.evl.common.exception.BussinessException;
import com.evl.common.persistence.dao.TrainSeatMapper;
import com.evl.common.persistence.dao.VehicleMapper;
import com.evl.common.persistence.model.TrainSeat;
import com.evl.common.persistence.model.Vehicle;
import com.evl.core.util.ToolUtil;
import com.evl.trainSeat.warpper.TrainSeatWarpper;

/**
 * 座位管理控制器
 *
 * @author 
 * @Date 2017-06-15 16:40:22
 */
@Controller
@RequestMapping("/trainSeat")
public class TrainSeatController extends BaseController {

    private String PREFIX = "/trainSeat/";

    @Resource
	VehicleMapper vehicleMapper;
    
    @Resource
    TrainSeatMapper trainSeatMapper;
    
    /**
     * 跳转到座位管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "trainSeat.html";
    }

    /**
     * 跳转到添加座位管理
     */
    @RequestMapping("/trainSeat_add/{vehicleId}")
    public String trainSeatAdd(@PathVariable Integer vehicleId, Model model) {
    	Vehicle vehicle = vehicleMapper.selectById(vehicleId);
    	model.addAttribute("vehicle", vehicle);
        return PREFIX + "trainSeat_add.html";
    }

    /**
     * 跳转到修改座位管理
     */
    @RequestMapping("/trainSeat_update/{trainSeatId}")
    public String trainSeatUpdate(@PathVariable Integer trainSeatId, Model model) {
    	TrainSeat trainSeat = this.trainSeatMapper.selectById(trainSeatId);
    	model.addAttribute("trainSeat",trainSeat);
    	if(trainSeat !=null){
    		Vehicle vehicle = vehicleMapper.selectById(trainSeat.getVehicleId());
        	model.addAttribute("vehicle", vehicle);
    	}
        return PREFIX + "trainSeat_edit.html";
    }

    /**
     * 获取座位管理列表
     */
    @RequestMapping(value = "/list/{vehicleId}")
    @ResponseBody
    public Object list(@PathVariable Integer vehicleId,@RequestParam(required = false) String carriageNo,@RequestParam(required = false) String seatNo,@RequestParam(required = false) String seatType) {
    	Page<Map<String, Object>> page = new PageFactory<Map<String, Object>>().defaultPage();
        List<Map<String, Object>> result = trainSeatMapper.selectTrainSeats(page,vehicleId,carriageNo,seatNo,seatType);
        page.setRecords((List<Map<String, Object>>)new TrainSeatWarpper(result).warp());
        return super.packForBT(page);
   	 
    }

    /**
     * 新增座位管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(TrainSeat trainSeat) {
    	if(ToolUtil.isOneEmpty(trainSeat,trainSeat.getSeatNo())){
    		throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
    	}
    	return this.trainSeatMapper.insert(trainSeat);
    }

    /**
     * 删除座位管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer trainSeatId) {
        return this.trainSeatMapper.deleteById(trainSeatId);
    }


    /**
     * 修改座位管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(TrainSeat trainSeat) {
    	if (ToolUtil.isEmpty(trainSeat) || trainSeat.getId() == null) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
    	
    	return this.trainSeatMapper.updateById(trainSeat);
    }

    /**
     * 座位管理详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail(@RequestParam Integer trainSeatId) {
        return this.trainSeatMapper.selectById(trainSeatId);
    }
}
